/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import Vue from 'vue'
import dayjs from 'dayjs'
import { formatDate } from './filter'
/**
 * @param {String} fmt optional, define formatting format
 */
Vue.filter('formatDate', formatDate)

Vue.filter('duration', (startTime, endTime) => {
  if (!endTime) endTime = dayjs().format('YYYY-MM-DD HH:mm:ss')
  // const second = moment(endTime).diff(moment(startTime), 'second')
  // 计算两个时间相差秒数(时间差单位可以是years,months,days,minutes,seconds)
  const timeDiff = dayjs(endTime).diff(dayjs(startTime), 'seconds')
  // 小时
  let timeH = Math.floor(timeDiff / 3600)
  timeH = timeH < 10 ? '0' + timeH : timeH
  // 分钟
  let timeMinute = Math.floor((timeDiff % 3600) / 60)
  timeMinute = timeMinute < 10 ? '0' + timeMinute : timeMinute
  // 秒
  let timeSeconds = (timeDiff % 3600) % 60
  timeSeconds = timeSeconds < 10 ? '0' + timeSeconds : timeSeconds
  return timeH + ':' + timeMinute + ':' + timeSeconds
})
